package com.kaigejava.javanotebook.threaddemo.ruturndemo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author kaigejava
 * 使用线程池创建线程
 */
public class ThreadPoolDemo implements  Runnable{
    private int x = 0;
    @Override
    public void run() {
        x++;
        System.out.println("当前执行第："+x+"个。线程名称是："+Thread.currentThread().getName());
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //使用 Executor这个顶级接口的字类创建线程池
        ExecutorService pool = Executors.newSingleThreadExecutor();
        ThreadPoolDemo threadPoolDemo = new ThreadPoolDemo();
        for(int i = 0;i<5;i++){
            //向线程池中添加
          pool.submit(threadPoolDemo);
          //这里也可以使用execute
          //  pool.execute(threadPoolDemo);
        }
        //线程池停止
        pool.shutdown();
    }
}
